/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package server.boundary;

import server.entities.Account;
import server.entities.Transaction;
import server.entities.User;
import java.util.List;
import java.util.Map;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 *
 * @author efim
 */
@Stateless
public class TransactionFacade extends AbstractFacade<Transaction> {
    @PersistenceContext(unitName = "EFMPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public TransactionFacade() {
        super(Transaction.class);
    }
    
    public List<Transaction> getTransactions(Map<String,String> params)
    {
        CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();        
        Root<Transaction> transaction = cq.from(Transaction.class);
        for (String p : params.keySet()) 
        {
            switch (p) 
            {
                case "id":
                    cq = cq.where(transaction.get("id").in(params.get(p)));
                    break;
                case "name":
                    cq = cq.where(transaction.get("name").in(params.get(p)));
                    break;
                case "amount":
                    cq = cq.where(transaction.get("amount").in(params.get(p)));
                    break;
                case "account_ids":
                    cq = cq.where(transaction.get("accountId").in(params.get(p).split(",")));
                    break;
                case "type_id":
                    cq = cq.where(transaction.get("transactionTypeId").in(params.get(p)));
                    break;
            }
        }
        TypedQuery q = getEntityManager().createQuery(cq);
        if(params.containsKey("count"))
        {
            q.setMaxResults(Integer.parseInt(params.get("count")));
        }
        if(params.containsKey("offset"))
        {
            q.setFirstResult(Integer.parseInt(params.get("offset")));
        }
        List list = q.getResultList();
        return list;
        
    }
    
    public Transaction createTransaction(Map<String,String> params)
    {
        Transaction tr = new Transaction();
        for(String p:params.keySet())
        {
            switch(p)
            {                
                case "name":
                    tr.setName(params.get(p));
                    break;
                case "amount":
                    tr.setAmount(Double.parseDouble(params.get(p)));
                    break;
                case "account_id":                    
                    tr.setAccountId(Long.parseLong(params.get(p)));                   
                    break; 
                case "type_id":
                    tr.setTransactionTypeId(Long.parseLong(params.get(p)));
                    break;
            }
        }
        this.create(tr);
        return tr;
    }
    
}
